﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MensUnoShop.Models.Discount
{
    [Serializable]
    public class HandselDiscount : ITradeInDiscount
    {
        private string _title;
        private int _deprice;
        private int _totalprice;
        private Dictionary<int, int> _judgement;
        private Order _od;
        private Config.OrderDiscountType _sktype;
        public string TITLE { set { _title = value; } get { return _title; } }
        public int DEPRICE { set { _deprice = value; } get { return _deprice; } }
        public Config.OrderDiscountType SKTYPE { set { _sktype = value; } get { return _sktype; } }


        public HandselDiscount()
        {
            _title = "買千折百";
            _deprice = 0;
            _sktype = Config.OrderDiscountType.HanselDiscount;
        }


        public HandselDiscount(Order od)
            : this()
        {
            _totalprice = od.TOTALPRICE-od.DSKLIST.Sum(d=>d.DEPRICE);
            _od = od;
        }

        public void GetDiscount()
        {
            //看條件滿多少就是倍數折抵
            if (!_od.MLIST.Any(m => m.TYPE == Config.MarketType.Mangazine))
            {
                SenerPro.Logger log = new SenerPro.Logger(Config.logpath);
                _judgement = SalesKitFactory.GetHanselJudgement();
                _deprice = 0;
                var j = _judgement.FirstOrDefault();
                if (j.Key > 0 && j.Value > 0 && _totalprice >= j.Key)
                {
                    int per = _totalprice / j.Key;
                    _deprice = j.Value*per;
                }
            }
            else
                _deprice = 0;
        }

        public bool SetDiscount(string txid)
        {
            return true;
        }

        /// <summary>
        /// 交易失敗處理
        /// </summary>
        public void DealFail(string txid)
        {
        }
    }
}